/*
 * libustream: User-space stackable streams
 * @(#) $Id: factory.c 2 2009-02-20 09:52:11Z mo@ilaven.net $
 */

/*
 * Copyright (c) 2002, 2008 Mo McRoberts.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The names of the author(s) of this software may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * AUTHORS OF THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "p_usmodules.h"

static com_result_t __stdcall UStream_BuiltinFactory_QueryInterface(IUStreamFactory *intf, com_riid_t riid, void **out);
static uint32_t __stdcall UStream_BuiltinFactory_AddRef(IUStreamFactory *intf);
static uint32_t __stdcall UStream_BuiltinFactory_Release(IUStreamFactory *intf);
static com_result_t __stdcall UStream_BuiltinFactory_CreateStreamNode(IUStreamFactory *intf, com_rclsid_t clsid, IUStream *stream, IUStreamNode **out);
static com_result_t __stdcall UStream_BuiltinFactory_CreateStreamHead(IUStreamFactory *intf, com_rclsid_t clsid, const iri_t *iri, int oflag, mode_t mode, IUStreamNode **out, int *fdout);

DECLARE_CLASS(UStream_BuiltinFactory)
{
	DECLARE_IMPLEMENTS(UStream_BuiltinFactory, IUStreamFactory)
};

DEFINE_CLASS_INTERFACE(UStream_BuiltinFactory, IUStreamFactory)
{
	UStream_BuiltinFactory_QueryInterface,
	UStream_BuiltinFactory_AddRef,
	UStream_BuiltinFactory_Release,
	UStream_BuiltinFactory_CreateStreamNode,
	UStream_BuiltinFactory_CreateStreamHead
};

DEFINE_STATIC_CLASS(UStream_BuiltinFactory, static_UStream_BuiltinFactory)
{
	DEFINE_STATIC_IMPLEMENTATION(UStream_BuiltinFactory, IUStreamFactory)
};

int
us__module_register_builtin(void)
{
	union
	{
		IUStreamFactory *factory;
		IUnknown *unk;
	} fact;
	
	/* Initialise the static class factory */
	INITIALISE_ADDREF_INTERFACE_POINTER(UStream_BuiltinFactory, &static_UStream_BuiltinFactory, IUStreamFactory);
	fact.factory = GET_INTERFACE_POINTER(&static_UStream_BuiltinFactory, IUStreamFactory);
	/* Register the factory for each of the handlers we support */
	com_register_factory(&CLSID_UStream_File, COM_CTX_ALL, NULL, fact.unk, NULL);
	com_register_factory(&CLSID_UStream_FD, COM_CTX_ALL, NULL, fact.unk, NULL);
	com_register_factory(&CLSID_UStream_Rot13, COM_CTX_ALL, NULL, fact.unk, NULL);
	/* Register the handlers with ustream itself */
	us_module_register("file", &CLSID_UStream_File);
	us_module_register("fd", &CLSID_UStream_FD);
	us_module_register("rot13", &CLSID_UStream_Rot13);
	return 0;
}

static com_result_t __stdcall
UStream_BuiltinFactory_QueryInterface(IUStreamFactory *intf, com_riid_t riid, void **out)
{
	if(com_guid_equal(riid, &IID_IUnknown) || com_guid_equal(riid, &IID_IUStreamFactory))
	{
		IUStreamFactory_AddRef(intf);
		*out = intf;
		return COM_S_OK;
	}
	return COM_E_NOINTERFACE;
}

static uint32_t __stdcall
UStream_BuiltinFactory_AddRef(IUStreamFactory *intf)
{
	(void) intf;
	
	return 2;
}

static uint32_t __stdcall
UStream_BuiltinFactory_Release(IUStreamFactory *intf)
{
	(void) intf;
	
	return 1;
}

static com_result_t __stdcall
UStream_BuiltinFactory_CreateStreamNode(IUStreamFactory *intf, com_rclsid_t clsid, IUStream *stream, IUStreamNode **out)
{
	(void) intf;

	if(com_guid_equal(clsid, &CLSID_UStream_Rot13))
	{
		return us__rot13_push(stream, out);
	}
	return COM_E_NOT_SUPPORTED;
}

static com_result_t __stdcall
UStream_BuiltinFactory_CreateStreamHead(IUStreamFactory *intf, com_rclsid_t clsid, const iri_t *iri, int oflag, mode_t mode, IUStreamNode **out, int *fdout)
{
	(void) intf;
	
	if(com_guid_equal(clsid, &CLSID_UStream_File))
	{
		return us__file_open(iri, oflag, mode, out, fdout);
	}
	if(com_guid_equal(clsid, &CLSID_UStream_FD))
	{
		return us__fd_open(iri, oflag, mode, out, fdout);
	}
	return COM_E_NOT_SUPPORTED;
}
